package com.stardata.liangshisq.tcslmon.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.stardata.liangshisq.tcslmon.annotation.IgnoreAuth;
import com.stardata.liangshisq.tcslmon.entity.DbiEmp;
import com.stardata.liangshisq.tcslmon.service.DbiEmpService;
import com.stardata.liangshisq.tcslmon.service.SysParameterService;
import com.stardata.liangshisq.tcslmon.service.TokenService;
import com.stardata.liangshisq.tcslmon.util.ApiBaseAction;
import com.stardata.liangshisq.tcslmon.util.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

/**
 * @author Samson Shu
 * @email shush@stardata.top
 * @date 2020/8/7 21:27
 */
@Api(tags = "登录授权接口")
@RestController
@RequestMapping("/v1/auth/")
@AllArgsConstructor
public class AuthControllerV1 extends ApiBaseAction {
  private final Logger logger = LoggerFactory.getLogger(this.getClass());

  private final DbiEmpService empService;
  private final TokenService tokenService;
  private final SysParameterService parameterService;

  @GetMapping("/shopid")
  @IgnoreAuth
  @ApiOperation(value = "获得系统店铺ID")
  public Object shopid(){
    String shopId = parameterService.getByCode("shopId");
    return success(shopId);
  }

    /**
     * <pre>
     * 后厨PC界面接口
     * </pre>
     */
    @PostMapping("/login")
    @IgnoreAuth
    @ApiOperation(value = "PC登录接口")
    public Object login(){

      JsonNode jsonParam = this.getJsonRequest();
      Long shopId = JSONUtil.getLong(jsonParam, "shopId");
      String empNo = jsonParam.get("empNo").asText();
      String password = jsonParam.get("password").asText();
      if (shopId == null) {
        return fail(HttpStatus.BAD_REQUEST, "登录失败: none shopId");
      }
      if (StringUtils.isBlank(empNo)) {
        return fail(HttpStatus.BAD_REQUEST, "登录失败: none empNo");
      }
      if (StringUtils.isBlank(password)) {
        return fail(HttpStatus.BAD_REQUEST, "登录失败: none password");
      }

      DbiEmp emp = empService.verifyEmpLogin(shopId, empNo, password);
      if (emp == null) {
        return fail(HttpStatus.BAD_REQUEST, "工号或密码错误！");
      }

      //创建登录令牌
      Map<String, Object> tokenMap = tokenService.createToken(emp.getId());
      String token = MapUtils.getString(tokenMap, "token");

      if (StringUtils.isBlank(token)) {
        return fail(HttpStatus.BAD_REQUEST, "登录失败：令牌创建");
      }

      //返回结果给前端
      return Objects.requireNonNull(success().put("token", token))
          .put("user", emp);
    }

}
